package me.zixing.memorandum.domain;

import me.zixing.memorandum.utils.LunarUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class MonthDO {
    private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
    private Date date;
    private List<DayDO> dayDOS = new ArrayList<>();
    private String monthForGregorian;
    private String monthForLunar;

    private MonthDO(){}

    public MonthDO nextMonthDO(){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(this.date);
        calendar.add(Calendar.MONTH,1);
        MonthDO nextMonthDO = buildMonthDO(calendar);
        return nextMonthDO;
    }

    public MonthDO previousMonthDO(){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(this.date);
        calendar.add(Calendar.MONTH,-1);
        MonthDO previousMonthDO = buildMonthDO(calendar);
        return previousMonthDO;
    }

    public MonthDO toAMonth(int space){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(this.date);
        calendar.add(Calendar.MONTH,space);
        MonthDO aMonthDO = buildMonthDO(calendar);
        return aMonthDO;
    }

    public static MonthDO buildMonthDO(Calendar calendar){
        MonthDO monthDO = new MonthDO();
        monthDO.setDate(calendar.getTime());
        monthDO.setMonthForLunar(getLunarMonth(monthDO.getDate()));
        int monthLength = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        calendar.set(Calendar.DAY_OF_MONTH,1);
        DayDO dayDO = DayDO.buildDayDO(calendar);
        List<DayDO> dayDOS = monthDO.getDayDOS();
        for(int i=1;i<=monthLength;i++){
            dayDOS.add(dayDO);
            dayDO = dayDO.nextDay();
        }
        return monthDO;
    }

    public static MonthDO buildMonthDO(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        MonthDO monthDO = new MonthDO();
        monthDO.setDate(calendar.getTime());
        monthDO.setMonthForLunar(getLunarMonth(monthDO.getDate()));
        int monthLength = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        calendar.set(Calendar.DAY_OF_MONTH,1);
        DayDO dayDO = DayDO.buildDayDO(calendar);
        List<DayDO> dayDOS = monthDO.getDayDOS();
        for(int i=1;i<=monthLength;i++){
            dayDOS.add(dayDO);
            dayDO = dayDO.nextDay();
        }
        return monthDO;
    }

    public static String getLunarMonth(Date date){
        int year = Integer.valueOf(new SimpleDateFormat("yyyy").format(date));
        int month = Integer.valueOf(new SimpleDateFormat("MM").format(date));
        int day = Integer.valueOf(new SimpleDateFormat("dd").format(date));
        String lunarString = LunarUtils.oneDayWithYearAndMonth(year,month,day);
        return lunarString;
    }

    public List<DayDO> getDayDOS() {
        return dayDOS;
    }

    private void setDayDOS(){

    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public String getMonthForGregorian() {
        return simpleDateFormat.format(this.date);
    }

    public void setMonthForGregorian(String monthForGregorian) {
        this.monthForGregorian = monthForGregorian;
    }

    public String getMonthForLunar() {
        return monthForLunar;
    }

    public void setMonthForLunar(String monthForLunar) {
        this.monthForLunar = monthForLunar;
    }

    @Override
    public String toString() {
        return "MonthDO{" +
                "date=" + date +
                ", dayDOS=" + dayDOS +
                ", monthForGregorian='" + monthForGregorian + '\'' +
                ", monthForLunar='" + monthForLunar + '\'' +
                '}';
    }
}
